﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using JMY_Models;
using System.Data.SqlClient;
using System.Data;
using Newtonsoft.Json;

namespace JMY_DAL
{
    public class T_OrderForm_Infos_DAL : JMY_IDAL.T_OrderForm_Infos_IDAL
    {
        SqlConnection conn = new SqlConnection("Data Source=.;Initial Catalog=HospitalMessage;Integrated Security=True");
        /// <summary>
        /// 获取医院
        /// </summary>
        /// <returns></returns>
        public List<T_Doctor_Infos_Model> GetHospital()
        {
            string sql = "select T_Doc_ID,T_Doc_Name from T_Doctor_Infos";
            SqlDataAdapter data = new SqlDataAdapter(sql,conn);
            DataTable table = new DataTable();
            data.Fill(table);
            var result = JsonConvert.DeserializeObject<List<T_Doctor_Infos_Model>>(JsonConvert.SerializeObject(table));
            return result;
        }

      
        /// <summary>
        /// 获取状态
        /// </summary>
        /// <returns></returns>
        public List<T_OrderForm_State_Infos_Model> GetState()
        {
            string sql = "select * from T_OrderForm_State_Infos";          
            SqlDataAdapter data = new SqlDataAdapter(sql,conn);
            DataTable table = new DataTable();
            data.Fill(table );
            var result = JsonConvert.DeserializeObject<List<T_OrderForm_State_Infos_Model>>(JsonConvert.SerializeObject(table));
            return result;
        }

        /// <summary>
        /// 订单显示
        /// </summary>
        /// <returns></returns>
        public List<T_OrderForm_Infos_Model> OrderFormShow(int sid,int yid,string stime,string etime)
        {
            
            try
            {
                
                string sql = "";
                if (sid==0&&yid==0)
                {
                    sql = "select * from T_OrderForm_Infos o INNER JOIN T_OrderForm_State_Infos s ON o.T_OrderForm_State=s.T_OrderForm_State_ID INNER JOIN T_Doctor_Combo d ON o.T_OrderForm_Combo=d.T_Combo_ID INNER JOIN T_Doctor_Infos t ON o.T_OrderForm_Doctor=t.T_Doc_ID INNER JOIN T_Doctor_Combo a ON o.T_OrderForm_Price=a.T_Combo_ID INNER JOIN T_Discount_Infos l ON o.T_OrderForm_Discount=l.T_Discount_ID where T_OrderForm_Time>='2001-1-30' and T_OrderForm_Time<='2999-1-6'";

                }
                else
                {

                    sql = "select * from T_OrderForm_Infos o INNER JOIN T_OrderForm_State_Infos s ON o.T_OrderForm_State=s.T_OrderForm_State_ID INNER JOIN T_Doctor_Combo d ON o.T_OrderForm_Combo=d.T_Combo_ID INNER JOIN T_Doctor_Infos t ON o.T_OrderForm_Doctor=t.T_Doc_ID INNER JOIN T_Doctor_Combo a ON o.T_OrderForm_Price=a.T_Combo_ID INNER JOIN T_Discount_Infos l ON o.T_OrderForm_Discount=l.T_Discount_ID where T_OrderForm_State='"+sid+ "' or T_OrderForm_Doctor='" + yid+ "' or (T_OrderForm_Time>='"+stime+ "'and T_OrderForm_Time<='"+etime+"')";
                }
               

                SqlDataAdapter data = new SqlDataAdapter(sql, conn);
                DataTable table = new DataTable();
                data.Fill(table);
                var result = JsonConvert.DeserializeObject<List<T_OrderForm_Infos_Model>>(JsonConvert.SerializeObject(table));
                return result;
                

            }
            catch (Exception ex)
            {

                throw ex;
            }
           
            
        }
    }
}
